
class Borg(object):
    _state = {}
    def __new__(cls, *args, **kw):
        ob = super(Borg, cls).__new__(cls, *args, **kw)
        ob.__dict__ = cls._state
        return ob
class C:
    _state = {}
    def __new__(cls, *args, **kwargs):
        ob = super(C,cls).__new__(cls,*args,**kwargs)
        ob.__dict__ = cls._state
        return ob
class MyClass2(Borg):
    a = 1

class Singleton(object):
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            orig = super(Singleton, cls)
            cls._instance = orig.__new__(cls, *args, **kw)
        return cls._instance

class MyClass(Singleton):
    a = 1
if __name__ == '__main__':
    # M = MyClass2()
    # M.p = 1
    # print(M.p)
    # D = MyClass2()
    # print(D.p)
    # ==============
    # v = C()
    # v.p = 1
    # print(v.p)
    # m = C()
    # print(m.p)
    # ============
    r = MyClass()
    print(r.a)
    g = MyClass()
    print(g.a)
    print(id(r),id(g))
    # ____________
    # g = Singleton()
    # v = Singleton()
    # print(id(g)==id(v))
